package com.hmy.common.json;

import com.hmy.common.exception.ExceptionType;
import com.hmy.common.exception.ServerException;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;

import java.util.Map;


/**
 * 
 * <pre>
 * <一句话功能简述>
 * JSON转化的工具类
 * 
 * <功能详细描述>
 * </pre>
 * 
 * @author xiongwei
 * @version [版本号, 2023年11月15日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class CcpJacksonkMapper
{
    private static final ObjectMapper objectMapper = new ObjectMapper();
    
    /**
     * 获取 objectmapper
     * 
     * @return 返回 objectmapper
     */
    public static ObjectMapper getObjectMapper()
    {
        return objectMapper;
    }
    
    /**
     * 
     * <pre>
     * <一句话功能简述>
     * 将字符串解析成Map对象
     * <功能详细描述>
     * </pre>
     * 
     * @param str json字符串
     * @return Map<String,String>
     * @throws ServerException
     * @see [类、类#方法、类#成员]
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> convertStringToMap(String str)
        throws ServerException
    {
        if (StringUtils.isEmpty(str))
        {
            throw new ServerException(ExceptionType.MISSING_SERVLET_REQUEST_PARAMETER);
        }
        
        try
        {
            return objectMapper.readValue(str, Map.class);
        }
        catch (Exception e)
        {
            throw new ServerException(ExceptionType.SERVER_ERROR, e);
        }
    }
    
    /**
     * 
     * <pre>
     * <一句话功能简述>
     * 将字符串解析成Map对象
     * <功能详细描述>
     * </pre>
     * 
     * @param str json字符串
     * @return Map<String,String>
     * @throws ServerException
     * @see [类、类#方法、类#成员]
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> convertStringToMapObject(String str)
        throws ServerException
    {
        if (StringUtils.isEmpty(str))
        {
            throw new ServerException(ExceptionType.MISSING_SERVLET_REQUEST_PARAMETER);
        }
        
        try
        {
            return objectMapper.readValue(str, Map.class);
        }
        catch (Exception e)
        {
            throw new ServerException(ExceptionType.SERVER_ERROR, e);
        }
    }
    
    
    /**
     * 
     * <pre>
     * <一句话功能简述>
     * 将字符串解析成Object对象
     * <功能详细描述>
     * </pre>
     * 
     * @param str json字符串
     * @param valueType 转换成对象
     * @return Object
     * @throws ServerException
     * @see [类、类#方法、类#成员]
     */
    public static Object convertStringToObject(String str, Class<?> valueType)
        throws ServerException
    {
        if (StringUtils.isEmpty(str) || valueType == null)
        {
            throw new ServerException(ExceptionType.MISSING_SERVLET_REQUEST_PARAMETER);
        }
        
        try
        {
            return objectMapper.readValue(str, valueType);
        }
        catch (Exception e)
        {
            throw new ServerException(ExceptionType.SERVER_ERROR, e);
        }
    }
    
    /**
     * 
     * <pre>
     * <一句话功能简述>
     * 将Object转换成字符串
     * <功能详细描述>
     * </pre>
     * 
     * @param  value
     * @return String
     * @throws ServerException
     * @see [类、类#方法、类#成员]
     */
    public static String convertObjectToString(Object value)
        throws ServerException
    {
        if (value == null)
        {
            throw new ServerException(ExceptionType.MISSING_SERVLET_REQUEST_PARAMETER);
        }
        
        try
        {
            return objectMapper.writeValueAsString(value);
        }
        catch (Exception e)
        {
            throw new ServerException(ExceptionType.SERVER_ERROR, e);
        }
        
    }

}
